/**
 * Title: jFamilyTree provider
 * Description: jFamilyTree Provider
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.provider.xml.dom;

import javax.swing.JOptionPane;

import org.xml.sax.SAXParseException;

/**
 * Error dialog
 * 
 * @author Bernard Bou
 */
public class ErrorDialog extends ParseErrorLogger
{
	boolean skip = false;

	/**
	 * Constructor
	 */
	public ErrorDialog()
	{
		super();
	}

	/**
	 * Put message
	 * 
	 * @param thisLevel
	 *        level
	 * @param thisType
	 *        type
	 * @param thisSAXException
	 *        sax exception
	 * @throws SAXParseException
	 */
	private void message(String thisLevel, int thisType, SAXParseException thisSAXException) throws SAXParseException
	{
		String thisLineArray[] = new String[6];
		thisLineArray[0] = thisLevel;
		thisLineArray[1] = "System Id / Public Id: " + thisSAXException.getSystemId() + " / " + thisSAXException.getPublicId();
		thisLineArray[2] = "Line:" + thisSAXException.getLineNumber() + " Column:" + thisSAXException.getColumnNumber();
		thisLineArray[3] = "";
		thisLineArray[4] = thisSAXException.getMessage();

		if (thisType != JOptionPane.INFORMATION_MESSAGE)
		{
			if (!skip)
			{
				thisLineArray[5] = "Press 'Yes' to continue parsing.";
				int thisAnswer = JOptionPane.showConfirmDialog(null, thisLineArray, "Continue parsing (Cancel to skip all)?", JOptionPane.YES_NO_CANCEL_OPTION, thisType);
				switch (thisAnswer)
				{
				case JOptionPane.YES_OPTION:
					return;
				case JOptionPane.CANCEL_OPTION:
					skip = true;
					return;
				case JOptionPane.NO_OPTION:
					throw thisSAXException;
				}
			}
		}
		else
		{
			thisLineArray[5] = "This error is not recoverable.";
			JOptionPane.showMessageDialog(null, thisLineArray, "Fatal XML Error", thisType);
			throw thisSAXException;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorLogger#terminate()
	 */
	public void terminate()
	{
		super.terminate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorLogger#warning(org.xml.sax.SAXParseException)
	 */
	public void warning(SAXParseException e) throws SAXParseException
	{
		super.warning(e);
		message("Warning reported by XML parser", JOptionPane.WARNING_MESSAGE, e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorLogger#error(org.xml.sax.SAXParseException)
	 */
	public void error(SAXParseException e) throws SAXParseException
	{
		super.error(e);
		message("Non-fatal Error reported by XML parser", JOptionPane.ERROR_MESSAGE, e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorLogger#fatalError(org.xml.sax.SAXParseException)
	 */
	public void fatalError(SAXParseException e) throws SAXParseException
	{
		super.fatalError(e);
		message("Fatal Error reported by XML parser", JOptionPane.INFORMATION_MESSAGE, e);
	}
}
